package org.webautotest.reservation;

import org.openqa.selenium.By;
import cn.bmyi.webautotest.aimee.BrowserEmulator;
import cn.bmyi.webautotest.aimee.BrowserEmulator.Element;
import cn.bmyi.webautotest.common.Config;

/**
 * 封装下体检预约单页面操作及页面元素
 * @author gaowen
 *
 */
public class ReservationPage
{
	public BrowserEmulator browser;
	public String url;
	public static class PageElement
	{
		public static final String SEARCH_TITLE_XPATH="/html/body/div/header/div[1]";//标题
		public static final String SEARCH_LOGO_XPATH="/html/body/div/header/img";//瑞慈Logo
		public static final String SEARCH_SELECTCITY_ID="city";//城市选择栏
		public static final String SEARCH_SELECTINSTITUTION_ID="institution";//机构选择栏
		public static final String SEARCH_INSTITUTIONLIST_XPATH="/html/body/div/div[1]/div[2]/div/span";//机构列表
		public static final String SEARCH_LASTINSTITUTION_XPATH="/html/body/div/div[1]/div[2]/div/span[10]";
		public static final String SEARCH_SELECTDATE_ID="datePicker";//日期选择栏
		public static final String SEARCH_CALENDARS_ID="dateWrap";
		public static final String SEARCH_DATE_XPATH="//*[@id=\"dateWrap\"]/div/table[2]/tbody/tr/td";//日期栏
		public static final String SEARCH_SELECTTIMEPERIOD_ID="timeSlot";//时间段选择栏
		public static final String SEARCH_TIMEPERION_ID="Code_AM_5";//第五个时间段
		public static final String SEARCH_INSTITLE_XPATH="/html/body/div/div[1]/div[5]/div[1]/div/h3";//所选机构名
		public static final String SEARCH_INSADDRESS_XPATH="/html/body/div/div[1]/div[5]/div[1]/div/p/span[1]";//机构地址
		public static final String SEARCH_INSPHONE_XPATH="/html/body/div/div[1]/div[5]/div[1]/div/p/span[2]";//机构电话
		public static final String SEARCH_TEL_XPATH="/html/body/div/div[1]/div[5]/div[1]/a";//电话图标
		public static final String SEARCH_NEARTRANS_XPATH="/html/body/div/div[1]/div[5]/div[2]/span";//机构附近交通
		public static final String SEARCH_RESERVATEBUTTON_XPATH="/html/body/div/div[1]/a";//立即预约按钮
		public static final String SEARCH_POPBOX_XPATH="/html/body/div[3]";//弹窗
		public static final String SEARCH_POPTITLE_XPATH="/html/body/div[3]/div[1]";//弹窗标题
		public static final String SEARCH_POPBUTTON_XPATH="/html/body/div[3]/div[3]";//返回预约列表按钮
		public static final String SEARCH_NEXTPAGEBUTTON_XPATH="//*[@id=\"dateWrap\"]/div/table[1]/tbody/tr/td[3]";//日历向右翻页按钮
		public static final String SEARCH_FORMALPAGEBUTTON_XPATH="//*[@id=\"dateWrap\"]/div/table[1]/tbody/tr/td[1]";//日历向左翻页按钮
		public static final String SEARCH_CITYLIST_XPATH="/html/body/div/div[1]/div[1]/div/span";//城市列表
		public static final String SEARCH_CAPTION_XPATH="//*[@id=\"dateWrap\"]/div/table[1]/tbody/tr/td[2]";//日历弹窗标题
		public static final String SEARCH_YEAR_XPATH="//*[@id=\"dateWrap\"]/div/table[4]/tbody/tr/td";//年
		public static final String SEARCH_MONTH_XPATH="//*[@id=\"dateWrap\"]/div/table[3]/tbody/tr/td";//月
		public static final String SEARCH_FIRSTINS_XPATH="/html/body/div/div[1]/div[2]/div/span[1]";//第一个机构
		public static final String SEARCH_CLEARDATEBUTTON_XPATH="//*[@id=\"dateWrap\"]/div/table[5]/tbody/tr/td[2]";//清空日期按钮
	}
	
	public ReservationPage(BrowserEmulator browser)
	{
		this.browser=browser;
	}
	
	/**
	 * 获取标题栏标题
	 * @return 标题
	 */
	public String getTitle()
	{
		return browser.findElement("xpath", PageElement.SEARCH_TITLE_XPATH).getText();
	}
	
	/**
	 * 判断Logo是否显示
	 * @return True:显示；False：未显示
	 */
	public boolean isLogeDisPlay()
	{
		try 
		{
			browser.findElement("xpath", PageElement.SEARCH_LOGO_XPATH);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 展开城市列表
	 * @throws InterruptedException 
	 */
	public void displayCity() throws InterruptedException
	{
		Element ins=browser.new Element(By.xpath(PageElement.SEARCH_FIRSTINS_XPATH));
		ins.waitUntilDisplay();
		browser.findElement("id",PageElement.SEARCH_SELECTCITY_ID).click();
		Element city=browser.new Element(By.xpath(PageElement.SEARCH_LOGO_XPATH));
		city.waitUntilConceal();
	}
	
	
	/**
	 * 选择机构
	 * @param instution 机构名
	 */
	public void chooseInstitution(String instution) 
	{
		Element Lastinstution=browser.new Element(By.xpath(PageElement.SEARCH_LASTINSTITUTION_XPATH));
		Lastinstution.waitUntilDisplay();
		Element[] cityList=browser.findElements("xpath", PageElement.SEARCH_INSTITUTIONLIST_XPATH);
		for (int i=0;i<cityList.length;i++)
		{
			if(cityList[i].getText().equals(instution))
			{
				cityList[i].click();
			}
		}
	}
	
	/**
	 * 选择最近日期
	 * @throws InterruptedException 
	 */
	public void chooseLatestDate() 
	{
		Element[] dateList=browser.findElements("xpath", PageElement.SEARCH_DATE_XPATH);
		for (int i=0;i<dateList.length;i++)
		{
			if(dateList[i].getAttribute("class").equals("dp_selected")||dateList[i].getAttribute("class").equals("dp_weekend dp_selected"))
			{
				dateList[i].click();
			}
		}
	}
	
	/**
	 * 选择时间段
	 * @throws InterruptedException 
	 */
	public void chooseTimePeriod() throws InterruptedException 
	{
		Element ins=browser.new Element(By.id(PageElement.SEARCH_TIMEPERION_ID));
		ins.waitUntilDisplay();
		Thread.sleep(500);
		browser.findElement("id",PageElement.SEARCH_TIMEPERION_ID).click();
		try
		{
			ins.waitUntilConceal();
		}
		catch (Exception e)
		{
			browser.findElement("id",PageElement.SEARCH_TIMEPERION_ID).click();
		}
		
	}
	
	/**
	 * 获取所选机构名
	 * @return 机构名
	 */
	public String getInsTitle()
	{
		
		return browser.findElement("xpath", PageElement.SEARCH_INSTITLE_XPATH).getText();
	}
	
	/**
	 * 获取所选机构地址
	 * @return 机构地址
	 */
	public String getInsAddress()
	{
		return browser.findElement("xpath", PageElement.SEARCH_INSADDRESS_XPATH).getText();
	}
	
	/**
	 * 获取所选机构电话
	 * @return 机构电话
	 */
	public String getInsPhone()
	{
		return browser.findElement("xpath", PageElement.SEARCH_INSPHONE_XPATH).getText();
	}
	
	/**
	 * 获取所选机构附近交通
	 * @return 附近交通
	 */
	public String getNearTransprot()
	{
		return browser.findElement("xpath", PageElement.SEARCH_NEARTRANS_XPATH).getText();
	}
	
	/**
	 * 获取立即预约按钮属性
	 * @return 立即预约按钮属性
	 */
	public String getReservationButtonAttribute()
	{
		return browser.findElement("xpath", PageElement.SEARCH_RESERVATEBUTTON_XPATH).getAttribute("class");
	}
	
	/**
	 * 点击立即预约按钮
	 */
	public void clickReservationButton()
	{
		By locator=By.xpath(PageElement.SEARCH_RESERVATEBUTTON_XPATH);
		Element married=browser.new Element(locator);
		married.waitUntilDisplay();
		browser.findElement("xpath", PageElement.SEARCH_RESERVATEBUTTON_XPATH).click();
	}
	
	
	/**
	 * 获取弹窗标题
	 * @return 弹窗标题
	 */
	public String getPopTitle()
	{
		return browser.findElement("xpath", PageElement.SEARCH_POPTITLE_XPATH).getText();
	}
	
	/**
	 * 点击弹窗按钮
	 */
	public void clickPopButton()
	{
		By locator=By.xpath(PageElement.SEARCH_POPBUTTON_XPATH);
		Element married=browser.new Element(locator);
		married.waitUntilDisplay();
		browser.findElement("xpath", PageElement.SEARCH_POPBUTTON_XPATH).click();
	}
	
	/**
	 * 下预约单
	 * @throws InterruptedException 
	 */
	public void finishReservation() throws InterruptedException
	{
		this.displayCity();
		this.chooseCity("上海");
		this.chooseInstitution("上海杨浦体检中心");
		this.chooseLatestDate();
		this.chooseTimePeriod();
		Thread.sleep(500);
		this.clickReservationButton();
		if (this.getPopTitle().equals("操作失败"))
		{
			String centerPageUrl=url=Config.getProperty("TestHost")+"/Html/PersonalCenter/reservations.html?v=2.2.0.0&token="+Config.getProperty("token");
			PersonalCenterPage personalCenterPage=new PersonalCenterPage(browser, centerPageUrl);
			personalCenterPage.clickImg1();
		}
		else
		{
			this.clickPopButton();
		}
	}
	
	/**
	 * 选择城市
	 * @param city 城市
	 */
	public void chooseCity(String city)
	{
		Element[] cityList=browser.findElements("xpath", PageElement.SEARCH_CITYLIST_XPATH);
		for (int i=0;i<cityList.length;i++)
		{
			if (cityList[i].getText().equals(city))
			{
				cityList[i].click();
			}
		}
	}
	
	/**
	 * 选择指定的年月日
	 * @param year 年份;例：2017
	 * @param month 月份;例：十月
	 * @param date 日期;例：12
	 */
	public void chooseDate(String year,String month,String date)
	{
		Element[] yearList=browser.findElements("xpath", PageElement.SEARCH_YEAR_XPATH);
		for(int i=0;i<yearList.length;i++)
		{
			if (yearList[i].getText().equals(year))
			{
				yearList[i].click();
				break;
			}
		}
		
		Element[] monthList=browser.findElements("xpath", PageElement.SEARCH_MONTH_XPATH);
		for (int i=0;i<monthList.length;i++)
		{
			if (monthList[i].getText().equals(month))
			{
				monthList[i].click();
				break;
			}
		}
		
		Element[] dateList=browser.findElements("xpath", PageElement.SEARCH_DATE_XPATH);
		for (int i=0;i<dateList.length;i++)
		{
			if (dateList[i].getText().equals(date))
			{
				dateList[i].click();
				break;
			}
		}
	}
	
	/**
	 * 点击日历栏标题
	 */
	public void clickCaption()
	{
		browser.findElement("xpath", PageElement.SEARCH_CAPTION_XPATH).click();
	}
	
	/**
	 * 等待logo消失
	 */
	public void waitLogoDespear()
	{
		By locator = By.xpath(PageElement.SEARCH_LOGO_XPATH);
		Element logo = browser.new Element(locator);
		logo.waitUntilConceal();
	}
	
	/**
	 * 等待城市下拉选择框收起
	 */
	public void waitCityChooseBoxDespear()
	{
		By locator = By.xpath(PageElement.SEARCH_FIRSTINS_XPATH);
		Element logo = browser.new Element(locator);
		logo.waitUntilConceal();
	}
	
	/**
	 * 等待日期栏清空按钮显示
	 */
	public void waitDateButtonDisplay()
	{
		By locator = By.xpath(PageElement.SEARCH_CLEARDATEBUTTON_XPATH);
		Element logo = browser.new Element(locator);
		logo.waitUntilDisplay();
	}
	
	/**
	 * 等待时间段显示
	 */
	public void waitTimePeriodDisplay()
	{
		By locator = By.id(PageElement.SEARCH_TIMEPERION_ID);
		Element logo = browser.new Element(locator);
		logo.waitUntilDisplay();
	}
	
	/**
	 * 点击日历栏向右翻页按钮
	 */
	public void clickNextPageButton()
	{
		browser.findElement("xpath", PageElement.SEARCH_NEXTPAGEBUTTON_XPATH).click();
	}
	
	/**
	 * 点击日历栏向左翻页按钮
	 */
	public void clickFormalPageButton()
	{
		browser.findElement("xpath", PageElement.SEARCH_FORMALPAGEBUTTON_XPATH).click();
	}
	
	/**
	 * 点击日历栏清空日期按钮
	 */
	public void clickClearButton()
	{
		browser.findElement("xpath", PageElement.SEARCH_CLEARDATEBUTTON_XPATH).click();
	}
}
